<?php
class database {

	public $host;
	public $db_name;
	public $user;
	public $pass;
	public $prefix;
	public $link;
	public $last_result;

	function __construct($host = false, $db_name = false, $user = false, $pass = false){
		if ($host && $db_name && $user){
			if (!$this->link = mysqli_connect($host, $user, $pass)){
				echo "Error conectando a la base de datos.";
				exit(); 
			}
			if (!mysqli_select_db($this->link, $db_name)){
				echo "Error seleccionando la base de datos.";
				exit(); 
			}
			$this->user = $user;
			$this->pass = $pass;
			$this->host = $user;
			$this->db_name = $user;
		}
		else{
			if (file_exists("admin/config.inc.php")){
				include("admin/config.inc.php");
				$this->host = $database["host"];
				$this->db_name = $database["db_name"];
				$this->user = $database["user"];
				$this->pass = $database["password"];
				$this->prefix = $database["prefix"];
		
				if (!$this->link = mysqli_connect($this->host, $this->user, $this->pass)){
					echo "Error conectando a la base de datos.";
					exit(); 
				}
				if (!mysqli_select_db($this->link, $this->db_name)){
					echo "Error seleccionando la base de datos.";
					exit(); 
				}
			}
			else{
				echo "Missing config";
			}
		}
	}
	
	function get_query($type, $table, $input, $conditions = false, $limit = false){
		if ($type == "select"){
			$in_string = "";
			foreach ($input as $key => $value){
				$in_string .= $value . ", ";
			}
			$in_string = substr($in_string, 0, -2);
			if (!$conditions){
				$query = "SELECT " . $in_string . " FROM " . $this->prefix . $table;
			}
			else{
				$cond_string = "";
				foreach ($conditions as $key => $value){
					$cond_string .= $key . "='" . $value . "' AND ";
				}
				$cond_string = substr($cond_string, 0, -5);
				$query = "SELECT " . $in_string . " FROM " . $this->prefix . $table . " WHERE " . $cond_string;
			}
			if ($limit != false) {
				$query .= " LIMIT " . $limit[0] . ", " . $limit[1];
			}
		}
		else if ($type == "insert"){
			$in_string = "";
			$in_values = "";
			foreach ($input as $key => $value){
				$in_string .= $key . ", ";
				$in_values .= "'" . $value . "', ";
			}
			$in_string = substr($in_string, 0, -2);
			$in_values = substr($in_values, 0, -2);
			
			$query = "INSERT INTO " . $this->prefix . $table . "(". $in_string . ") VALUES (" . $in_values . ")"; 
		}
		else if ($type == "delete"){ /////probar
			$in_string = "";
			foreach ($input as $key => $value){
				$in_string .= $key . " = '" . $value . "' AND ";
			}
			$in_string = substr($in_string, 0, -5);
			
			$query = "DELETE FROM " . $this->prefix . $table . " WHERE " . $in_string; 
		}
		else if ($type == "update"){ ////probar
			$in_string = "";
			foreach ($input as $key => $value){
				$in_string .= $key . " = '" . $value . "', ";
			}
			$in_string = substr($in_string, 0, -2);
			if (!$conditions){
				$query = "UPDATE " . $this->prefix . $table . " SET " . $in_string;
			}
			else{
				$cond_string = "";
				foreach ($conditions as $key => $value){
					$cond_string .= $key . " = '" . $value . "' AND ";
				}
				$cond_string = substr($cond_string, 0, -5);
				$query = "UPDATE " . $this->prefix . $table . " SET " . $in_string . " WHERE " . $cond_string;
			}
		}
		
		return $query;
	
	}
	
	function simple_query($query){
		return mysqli_query($this->link, $query);
	}
	
	function query($type, $table, $input, $conditions = false, $limit = false){
		$this->last_result = mysqli_query($this->link, $this->get_query($type, $table, $input, $conditions, $limit));
		return $this->last_result;
	}
	
	function get_num_rows($result = false){
		if ($result == false)
			return mysqli_num_rows($this->last_result);
		else
			return mysqli_num_rows($result);
	}
	
	function complete_fetch_array($result = false){
		$i = 0;
		if ($result == false){
			$rows = array();
			while ($row = $this->fetch_array($this->last_result)) {
				$rows[$i] = $row;
				$i++;
			}
		}
		else{
			$rows = array();
			while ($row = $this->fetch_array($result)) {
				$rows[$i] = $row;
				$i++;
			}
			
		}
		unset($i);
		return $rows;
	}
	
	function fetch_array($result = false){
		if ($result == false)
			return mysqli_fetch_array($this->last_result);
		else
			return mysqli_fetch_array($result);
		
	}
	
	/*function get_result_value($row, $name, $old_rows = false, $result = false){
		if ($old_rows == false){
			if ($result == false){
				$rows = $this->complete_fetch_array();
			}
			else{
				$rows = $this->complete_fetch_array($result);
			}
			return $rows[$row][$name];
		}
		else{
			return $old_rows[$row][$name];
		}
		
	}*/
	
	function connect_other($host, $db_name, $user, $pass){
		if (!$link = mysqli_connect($host, $user, $pass)){
			$valid = 0;
		}
		if (!mysqli_select_db($link, $db_name)){
			$valid = 0;
		}
		else{
			$valid = 1;
			$this->link = $link;
			$this->user = $user;
			$this->pass = $pass;
			$this->host = $user;
			$this->db_name = $user;
		}
		
		return $valid;
	}
	
	function multiquery($queries){
		if (mysqli_multi_query($this->link, $queries)) {
			$results = array();
			$i = 0;
			do {
				/* store first result set */
				$results[$i] = mysqli_store_result($this->link);
				$i = $i + 1;
			} while (@mysqli_next_result($this->link));
		}
		
		return $results;
	}
	
	function error(){
		return mysqli_error($this->link);
	}
	
}
?>